// store/menu.ts
import { defineStore } from "pinia";
import { getMenuList } from "@/store/menu";
import { filterAsyncRoutes } from "@/utils/route";
import { addRoutes } from "../router";
import type { MenuItem } from "../types/menu";

export const useMenuStore = defineStore("menu", {
  state: () => ({
    menuList: [] as MenuItem[],
    loaded: false,
  }),
  actions: {
    async generateRoutes() {
      try {
        const { data } = await getMenuList();
        this.menuList = filterAsyncRoutes(data);
        addRoutes(this.menuList);
        this.loaded = true;
      } catch (error) {
        this.loaded = false;
        throw error;
      }
    },
  },
});
